package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    //获取视频评论列表
    @Override
    public List findComments(String id, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(id))).
                skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return commentList;
    }

    @Override
    public String save(Video video) {
        //1、设置属性
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        //2、调用方法保存对象
        mongoTemplate.save(video);
        //3、返回对象id
        return video.getId().toHexString();
    }

    /**
     * 根据vid查询小视频列表
     */
    @Override
    public List<Video> findMovementsByVids(List<Long> vids) {
        Query query = Query.query(Criteria.where("vid").in(vids));
        return mongoTemplate.find(query, Video.class);
    }


    /**
     * 分页查询小视频数据
     */
    @Override
    public List<Video> queryVideoList(int page, Integer pagesize) {
        Query query = new Query().limit(pagesize).skip((page - 1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public PageResult findByUserId(Integer page, Integer pagesize, Long uid) {
        Query query = new Query(Criteria.where("userId").is(uid)).limit(pagesize).skip((page - 1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        long count = mongoTemplate.count(query, Video.class);
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        return new PageResult(page, pagesize, count, videoList);

    }

    @Override
    public void findUpdateByid(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        Video video = mongoTemplate.findOne(query, Video.class);
        //video.setLikeCount(video.getLikeCount()+1);
        Update update = Update.update("likeCount", video.getLikeCount() + 1);
        mongoTemplate.updateFirst(query, update, video.getClass());
    }

    @Override
    public void sendComments(Comment newcomment) {
        //根据动态id查询出被评论人id
        Query query = Query.query(Criteria.where("id").is(newcomment.getPublishId()));
        Video video = mongoTemplate.findOne(query, Video.class);
        newcomment.setPublishUserId(video.getUserId());
        mongoTemplate.save(newcomment);
    }

}